//
//  FSScrollerToolBar.m
//  fanshu
//
//  Created by mh on 16/3/27.
//  Copyright © 2016年 FS. All rights reserved.
//
#define scrollerViewBtnFont [UIFont systemFontOfSize:14.0f]
static CGFloat const margin = 10;

#import "FSScrollerToolBar.h"
#import "FSButton.h"
@interface FSScrollerToolBar ()
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,weak)UIView *contentView;

@property(nonatomic,strong)FSButton *seletedBtn;

@property(nonatomic,assign)BOOL contentLess2Width;
@end
@implementation FSScrollerToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *contentView = [[UIView alloc] init];
        [self addSubview:contentView];
        self.contentView = contentView;
        self.scrollEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.bounces = NO;
        self.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0f];
        self.contentLess2Width = NO;
        }
    return self;
}

+ (instancetype)ScrollerToolBarWithTitle:(NSArray<NSString *> *)titleArray{
    FSScrollerToolBar *scrollerTool = [[FSScrollerToolBar alloc] init];
    scrollerTool.titleArray = titleArray;
    return scrollerTool;
}

- (void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
    //创建对应个数的按钮
    for (int i = 0; i < titleArray.count; i ++) {
        FSButton *btn = [[FSButton alloc] init];
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        btn.titleLabel.font = scrollerViewBtnFont;
        [btn setTitleColor:[UIColor colorWithRed:174/255.0 green:174/255.0 blue:174/255.0 alpha:1.0f] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:215/255.0 green:137/255.0 blue:126/255.0 alpha:1.0f] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
    }
}

- (void)btnOnClick:(FSButton *)btn{
    self.seletedBtn.selected = NO;
    btn.selected = YES;
    self.seletedBtn = btn;
    if (self.btnOnClick) {
        self.btnOnClick(btn);
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat btnW = 0;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnH = self.height;
    
    if (self.contentLess2Width == YES) {
        //小于则重新设置位置和尺寸
        btnW = ScreenWidth / self.contentView.subviews.count;
        for (int i = 0; i < self.contentView.subviews.count; i++) {
            FSButton *btn = self.contentView.subviews[i];
            btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            btnX += btnW;
        }
    }else{
        //设置尺寸位置
        for (int i = 0; i < self.contentView.subviews.count; i++) {
            FSButton *btn = self.contentView.subviews[i];
            btnW = [btn.titleLabel.text sizeWithFont:scrollerViewBtnFont].width + margin * 2;
            btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            btnX += btnW;
        }
    }
    self.contentSize = CGSizeMake(btnX - 0.1, btnH);
    self.contentView.x = 0;
    self.contentView.y = 0;
    self.contentView.size = self.contentSize;
    
    if (self.contentSize.width < ScreenWidth && self.contentLess2Width == NO) {
        self.contentLess2Width = YES;
        [self setNeedsLayout];
    }
}

- (void)selectBtnWithTitle:(NSString *)title{
    for (FSButton *btn in self.contentView.subviews) {
        if ([btn.titleLabel.text isEqualToString:title]) {
            [self btnOnClick:btn];
            break;
        }
    }
}

@end
